#include "xSceneModel.h"
#include <osgText/Text>
#include <osgUtil/Optimizer>
#include <threadSafeUpdateCallback.h>

xSceneModel::xSceneModel(QObject *parent) :
    QObject(parent)
{
    createScene();
}

xSceneModel::~xSceneModel()
{

}

osg::Group *xSceneModel::getRootNode() const
{
    return mRoot;
}


bool xSceneModel::saveSceneData(const std::string &)
{
    return true;
}

void xSceneModel::createScene()
{
    // Init the main Root Node/Group
    initSceneNode();

    // Optimize the m_pTreeModel
    osgUtil::Optimizer optimizer;
    optimizer.optimize(mSceneGroup);
    optimizer.reset();

    //创建场景结构
    createSceneStruct();
}

void xSceneModel::initialViewerGroup()
{
    //初始化坐标文字
    osg::ref_ptr<osgText::Text> posText = new osgText::Text;
    osg::ref_ptr<osgText::Text> posText1 = new osgText::Text;
    osg::ref_ptr<osgText::Text> posText2 = new osgText::Text;
    osg::ref_ptr<osgText::Text> posText3 = new osgText::Text;
    std::string caiyun("fonts/simhei.ttf");
    posText->setFont(caiyun);

    posText1->setFont(caiyun);
    posText2->setFont(caiyun);
    posText3->setFont(caiyun);

    //设置文字显示的位置
    osg::Vec3 position(10.0f, 50.0f, 0.0f);
    posText->setPosition(position);
    position.y() -= 12;
    posText1->setPosition(position);
    position.y() -= 12;
    posText2->setPosition(position);
    position.y() -= 12;
    posText3->setPosition(position);

    //字体大小
    posText->setCharacterSize(8.0);
    posText1->setCharacterSize(8.0);
    posText2->setCharacterSize(8.0);
    posText3->setCharacterSize(8.0);

    //字体颜色
    posText->setColor(osg::Vec4( 1, 1, 1, 1));
    posText1->setColor(osg::Vec4( 1, 1, 1, 1));
    posText2->setColor(osg::Vec4( 1, 1, 1, 1));
    posText3->setColor(osg::Vec4( 1, 1, 1, 1));
    posText->setDataVariance(osg::Object::DYNAMIC);
    posText1->setDataVariance(osg::Object::DYNAMIC);
    posText2->setDataVariance(osg::Object::DYNAMIC);
    posText3->setDataVariance(osg::Object::DYNAMIC);

    //设置显示的文字
    posText->setText("Row:0 Col:0");
    posText1->setText("[X]:0.0");
    posText2->setText("[Y]:0.0");
    posText3->setText("[Z]:0.0");

    osg::ref_ptr<osg::Geode> textGeode = new osg::Geode();
    textGeode->addDrawable(posText);
    textGeode->addDrawable(posText1);
    textGeode->addDrawable(posText2);
    textGeode->addDrawable(posText3);

    //设置状态
    osg::StateSet* stateset = textGeode->getOrCreateStateSet();
    stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);//关闭灯光
    stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);//关闭深度

    //打开GL_BLEND混合模式（以保证Alpha纹理正确）
    stateset->setMode(GL_BLEND,osg::StateAttribute::ON);

    osg::ref_ptr<osg::Camera> textCamera = new osg::Camera;
    //设置透视矩阵
    textCamera->setProjectionMatrix(osg::Matrix::ortho2D(0,600,0,600));//
    //设置绝对参考坐标系，确保视图矩阵不会被上级节点的变换矩阵影响
    textCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    //视图矩阵为默认的
    textCamera->setViewMatrix(osg::Matrix::identity());
    //设置背景为透明，否则的话可以设置ClearColor
    textCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
    textCamera->setAllowEventFocus( false);//不响应事件，始终得不到焦点
    //设置渲染顺序，必须在最后渲染
    textCamera->setRenderOrder(osg::Camera::RenderOrder::POST_RENDER);

    osg::ref_ptr<CThreadSafeUpdateCallback> spCallback = dynamic_cast<CThreadSafeUpdateCallback*>(mRoot->getUpdateCallback());

    if (spCallback != NULL)
    {
        mViewerGroup->addChild(textCamera);
        textCamera->addChild(textGeode);
    }
}

void xSceneModel::createSceneStruct()
{
    //创建场景节点结构
    osg::ref_ptr<CThreadSafeUpdateCallback> spCallback = dynamic_cast<CThreadSafeUpdateCallback*>(mRoot->getUpdateCallback());
    // Add the m_pTreeModel to the scene
    if (spCallback != NULL)
    {
        mRoot->addChild(mSceneGroup.get());
        mRoot->addChild(mEditSceneGroup.get());
        mRoot->addChild(mViewerGroup.get());
        mRoot->addChild(mMapperGroup.get());
        mRoot->addChild(mSkyBoxGroup.get());
        mRoot->addChild(mTempGroup.get());
        //mRoot->addChild(mScopeGroup.get());

        initialViewerGroup();
        //InitialSkyBox(mSkyBoxGroup);//创建天空盒
    }
}

void xSceneModel::initSceneNode()
{
    mRoot  = new osg::Group;
    mRoot->addUpdateCallback(new CThreadSafeUpdateCallback());

    mSceneGroup = new osg::Group;
    mSceneGroup->setName("sceneGroup");
    mEditSceneGroup = new osg::Group;
    mEditSceneGroup->setName("editGroup");
    mViewerGroup = new osg::Group;
    mViewerGroup->setName("viewerGroup");
    mMapperGroup = new osg::Group;
    mMapperGroup->setName("mapperGroup");
    mSkyBoxGroup = new osg::Group;
    mSkyBoxGroup->setName("SkyBoxGroup");
    mTempGroup = new osg::Group;
    mTempGroup->setName("tempGroup");
    mTempGroup1 = new osg::Group;
    mTempGroup1->setName("tempGroup1");
    mScopeGroup = new osg::Group;
    mScopeGroup->setName("ScopeGroup");
}


